package com.yyx.app1.CommonControls.CloudComputing_Control;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yyx.app1.CommonControls.EditTextItemControl.EditTextItemControl;
import com.yyx.app1.CommonControls.EditTextItemControl.EditTextItemControl_;
import com.yyx.app1.CommonControls.EditTextItemControl.OnEditTextItemControl_EditTextChange_Listener;
import com.yyx.app1.CommonControls.MyViewPager.BackButton;
import com.yyx.app1.R;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;


/**
 * Created by yuexing on 2015/9/14.
 */
@EViewGroup(R.layout.view_cloudcomputing_control)
public class CloudComputing_Control extends LinearLayout {
    private CloudComputing_Control_Adapter adapter;
    private Context context;
    private EditTextItemControl[] requres;
    private TextView[] results;
    private List<HashMap<String,Object>> resultlist;

    @ViewById
    LinearLayout CloudComputingControl_require,CloudComputingControl_result;

    @ViewById
    TextView CloudComputingControl_title;

    @ViewById
    BackButton CloudComputingControl_home;

    public CloudComputing_Control(Context context) {
        super(context);
        this.context=context;
    }

    public CloudComputing_Control(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context=context;
    }

    public CloudComputing_Control(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context=context;
    }

    public void OnCreate(String[] edits, final String[] result,String title,CloudComputing_Control_Adapter adapter1)
    {
        this.adapter=adapter1;
        CloudComputingControl_title.setText(title);
        this.requres=new EditTextItemControl[edits.length];
        this.results=new TextView[result.length];
        for (int i = 0 ; i<result.length ; ++i)
        {
            TextView view = new TextView(context);
            this.results[i]=view;
            view.setText(result[i]);
            view.setTextSize(20);
            view.setPadding(20,20,20,20);
            view.setTextColor(Color.RED);
            view.setGravity(Gravity.CENTER);
            CloudComputingControl_result.addView(view);
        }
        for (int i = 0 ; i<edits.length ; ++i)
        {
            EditTextItemControl view = EditTextItemControl_.build(context);
            view.getControl_textview().setText(edits[i]);
            requres[i]=view;
            CloudComputingControl_require.addView(view);
        }
        OnEditTextItemControl_EditTextChange_Listener listener = new OnEditTextItemControl_EditTextChange_Listener() {
            @Override
            public void edit_text_change() {
                ArrayList<HashMap<String,String>> list = new ArrayList<>();
                for (int i = 0 ; i<requres.length ; ++i)
                {
                    String value = CloudComputing_Control.this.requres[i].getControl_edittext().getText().toString();
                    String key = CloudComputing_Control.this.requres[i].getControl_textview().getText().toString();
                    HashMap<String,String> map = new HashMap<>();
                    map.put("key",key);
                    map.put("value", value);
                    list.add(map);
                }
                adapter.backgroundComputing(list);
            }
        };
        for (int i=0 ; i<requres.length;++i)
        {
            CloudComputing_Control.this.requres[i].setEditTextChange_Listener(listener);
        }
    }

    @Click
    public void CloudComputingControl_home()
    {
        if(context instanceof Activity)
        {
            ((Activity) context).finish();
        }
    }

    public TextView[] getResults() {
        return results;
    }

    public LinearLayout getCloudComputingControl_require() {
        return CloudComputingControl_require;
    }

    public void setResultlist(List<HashMap<String, Object>> resultlist) {
        this.resultlist = resultlist;
    }
}
